﻿// Copyright (C) 2009 Choonghwan Lee (linjus.lee@gmail.com)
//
// This file is part of amanda.
//
// amanda is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// amanda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with amanda. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace manager
{
	/// <summary>
	/// Interaction logic for Wizard3Foobar2000.xaml
	/// </summary>
	public partial class Wizard3Foobar2000 : PageFunction<WizardResult>
	{
		private WizardData data;

		public Wizard3Foobar2000(WizardData data)
		{
			InitializeComponent();

			this.data = data;

			this.Loaded += new RoutedEventHandler(Wizard3Foobar2000_Loaded);
		}

		private void ReadPlaylist()
		{
			int count = -1;
			try {
				if (this.data.LibraryInfo != null)
					count = Global.MainWindow.ReadFoobar2000(this.data.LibraryInfo);
			}
			catch (Exception e) {
			}
			finally {
				this.FillDescription(count);
				this.buttonNext.IsEnabled = count != -1;
			}
		}

		private void FillDescription(int count)
		{
			if (this.data.LibraryInfo == null) return;

			string msg =
				"At the first step, you chose '" + this.data.LibraryInfo.Name + "'. " +
				"The foobar2000 playlist that corresponds to this library is '" + this.data.LibraryInfo.PlaylistName + "'.\r\n\r\n";
			if (count != -1) {
				msg += count + " songs were detected. You may want to check songs from the main window.";
			}
			else {
				msg +=
					"Run foobar2000, and make sure there is a playlist named '" + this.data.LibraryInfo.PlaylistName + "'. " +
					"Also, make sure that foobar2000 has the COM Automation Server.";
			}

			this.textDescription.Text = msg;
		}

		private void buttonCheck_Click(object sender, RoutedEventArgs e)
		{
			this.ReadPlaylist();
		}

		void Wizard3Foobar2000_Loaded(object sender, RoutedEventArgs e)
		{
			this.ReadPlaylist();
		}

		private void buttonNext_Click(object sender, RoutedEventArgs e)
		{
			this.OnReturn(new ReturnEventArgs<WizardResult>(WizardResult.FINISHED));
		}
	}
}
